package com.zhongxi.guoyuan.fragment.hmfragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.rewardCoin.ShopNewCateAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseFragment;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.coinStore.HotGoodsData;
import com.zhongxi.guoyuan.entity.coinStore.HotGoodsResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.view.rewardCoin.RewardCoinDetailActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.zhongxi.guoyuan.R.string.get;

/**
 * Created by Administrator on 2017/10/12.
 */

public class OneMNewFragment extends BaseFragment implements PullToRefreshBase.OnRefreshListener2 {

    @Bind(R.id.lvList)
    PullToRefreshListView lvList;
    private String trim;
    private String id;
    private List<HotGoodsData> data = new ArrayList<>();
    private ShopNewCateAdapter adapter;

    public OneMNewFragment() {
        System.out.println("OneMNewFragment====");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.println("OneMNewFragment===onCreate");
        System.out.println("getUserVisibleHint()==" + getUserVisibleHint());
        if (getArguments() != null) {
            Bundle arguments = getArguments();
            id = arguments.getString("id");
            trim = arguments.getString("trim");
            System.out.println("trim444444==" + trim);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        System.out.println("OneMNewFragment=====onCreateView");
        System.out.println("getUserVisibleHint()==" + getUserVisibleHint());
        View view = inflater.inflate(R.layout.fragment_goods_market, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        System.out.println("OneMNewFragment===onActivityCreated");
        System.out.println("getUserVisibleHint()==" + getUserVisibleHint());
    }

//    @Override
//    protected View setContentView(LayoutInflater inflater, ViewGroup container) {
//        System.out.println("OneMNewFragment===setContentView");
//        View view = inflater.inflate(R.layout.fragment_goods_market, container, false);
//        ButterKnife.bind(this, view);
//        return view;
//    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        System.out.println("isVisibleToUser==" + isVisibleToUser);
        System.out.println("OneMNewFragment==setUserVisibleHint");
        System.out.println("getUserVisibleHint()==" + getUserVisibleHint());
        if (isVisibleToUser) {
            if (TextUtils.isEmpty(trim)) {
                getGoodsList();
            } else {
                System.out.println("trim555==" + trim);
                getPostGoodsList();
            }
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        System.out.println("OneMNewFragment==onStart");
        System.out.println("getUserVisibleHint()==" + getUserVisibleHint());
    }

//    @Override
//    protected void lazyLoad() {
//        System.out.println("-------");
//        if (TextUtils.isEmpty(trim)) {
//            getGoodsList();
//        } else {
//            System.out.println("trim555==" + trim);
//            getPostGoodsList();
//        }
//    }

    @Override
    protected void initView() {
        super.initView();
        lvList.setMode(PullToRefreshBase.Mode.BOTH);
        adapter = new ShopNewCateAdapter(context, data);
        lvList.setAdapter(adapter);
        getEmpty();
    }

    private void getEmpty() {
        TextView tv = new TextView(context);
        tv.setGravity(Gravity.CENTER);
        tv.setText("暂无数据,敬请期待");
        tv.setTextSize(20);
        //当界面为空的时候显示的视图
        lvList.setEmptyView(tv);
    }

    @Override
    protected void initData() {
        super.initData();
        if (TextUtils.isEmpty(trim)) {
            getGoodsList();
        } else {
            System.out.println("trim555==" + trim);
            getPostGoodsList();
        }
    }

    private void getPostGoodsList() {
        Map<String, String> map = new HashMap<>();
        map.put("content", trim);
        map.put("shopid", "");
        map.put("cateid", id);
        map.put("goodstype", "1");
        map.put("orderBy", "0");
        String str = "&page=" + pageNum;
        getServer(Url.MALL_GOOD_SEARCH, map, str, false, null, "post");
    }

    private void getGoodsList() {
        String str = "&cateid=" + id + "&goodstype=1" + "&orderBy=0" + "&page=" + pageNum;
        getServer(Url.MALL_GOODS_LIST, null, str, false, null, "get");
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        lvList.setOnRefreshListener(this);
        lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(context, RewardCoinDetailActivity.class);
                intent.putExtra("gid", data.get(position - 1).getId());
                context.startActivity(intent);
            }
        });
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try {
            HotGoodsResult result = gson.fromJson(response, HotGoodsResult.class);
            if (result.getSafeobj() != null && !"".equals(result.getSafeobj())) {
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            }
            if (pageNum == 1) {
                data.clear();
            }
            if (null != result.getData() && result.getData().size() > 0) {
                data.addAll(result.getData());
            }
            adapter.notifyDataSetChanged();
            lvList.onRefreshComplete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        try {
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            if (null != result.getSafeobj() && !"".equals(result.getSafeobj())) {
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            }
            lvList.onRefreshComplete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        pageNum = 1;
        if (!TextUtils.isEmpty(trim)) {
            getPostGoodsList();
        } else {
            getGoodsList();
        }
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        pageNum++;
        if (!TextUtils.isEmpty(trim)) {
            getPostGoodsList();
        } else {
            getGoodsList();
        }
    }
}
